package com.oracle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.oracle.bean.LoginUser;
import com.oracle.entity.Oauser;
import com.oracle.mapper.OauserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private OauserMapper oauserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        System.out.println("username : " + username);
        //查询数据库 判断用户存在有效
        //先实现认证过程，去我们自己数据库查询当前用户数据
        LambdaQueryWrapper<Oauser> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Oauser::getLoginname,username);

        Oauser user = oauserMapper.selectOne(wrapper);

        //判断是否存在
        if(Objects.isNull(user)){
            throw new RuntimeException("用户名或者密码错误");
        }

        return new LoginUser(user);
    }
}
